﻿using System;
using System.Net;
using System.Windows;
using ClientLauncher.Src.ViewModels;
using Game500.Client.Src;
using Game500.ClientServerCommon.Src;

namespace ClientLauncher.Src
{
  public static class ClientLauncher
  {
    [STAThread]
    public static void Main(string[] args)
    {
      var view = new MainWindow();
      var mm = new BaseMapManager();

      var addr = IPAddress.Loopback;
      if (args.Length != 0) addr = IPAddress.Parse(args[0]);

      var cm = new ServerConnectionManager(new IPEndPoint(addr, 5050));
      try
      {
        var vm = new GameListViewModel(cm, mm);
        view.DataContext = vm;
        view.ShowDialog();

        if (view.DialogResult == true)
        {
          var game = new MainGame(vm.Result, vm.Username, mm);
          game.Run();
        }
      }
      catch (Exception e)
      {
        MessageBox.Show(e.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Stop);
      }
    }
  }
}